//===========================
// Filename: game.cpp
//===========================
#include "Game.h"


Game::Game(void) {
	m_SDL_Window = nullptr;
	m_SDL_Renderer = nullptr;
	m_SDL_GLcontext = nullptr;

	m_Running = true;
}


Game::~Game(void) {
}


void Game::Run() {
	bool check = true;

	check = Initialise();
	if(check == false) {
		return;
	}

	SDL_Event event;
	while(m_Running) {
		while(SDL_PollEvent(&event)) {
			OnEvent(&event);
		}

		Update();
		Render();
	}

	Shutdown();
}


bool Game::Initialise() {

	//
	m_ScreenWidth = 1000;
	m_ScreenHeight = 800;

	//
	// Initialise SDL
	//
	if(SDL_Init(SDL_INIT_EVERYTHING) == -1) {
		LogSDLError(std::cout, "SDL_Init");
		return false;
	}

	//
	// Initialise OpenGL Variables
	//
	SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
 
    SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
    SDL_GL_SetAttribute(SDL_GL_BUFFER_SIZE, 32);
 
    SDL_GL_SetAttribute(SDL_GL_ACCUM_RED_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_GREEN_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_BLUE_SIZE, 8);
    SDL_GL_SetAttribute(SDL_GL_ACCUM_ALPHA_SIZE, 8);
 
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
 
    SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 2);

	//
	// Create the Window
	//
	m_SDL_Window = SDL_CreateWindow("My Game Window",
		SDL_WINDOWPOS_UNDEFINED,
		SDL_WINDOWPOS_UNDEFINED,
		m_ScreenWidth,
		m_ScreenHeight,
		SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
	if(m_SDL_Window == nullptr) {
		LogSDLError(std::cout, "CreateWindow");
		return false;
	}

	//
	// Create the renderer
	//
	m_SDL_Renderer = SDL_CreateRenderer(m_SDL_Window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
	if(m_SDL_Renderer == nullptr) {
		LogSDLError(std::cout, "CreateRenderer");
		return false;
	}

	//
	// Create the Open GL Context
	//
	m_SDL_GLcontext = SDL_GL_CreateContext(m_SDL_Window);
	if(m_SDL_GLcontext == nullptr) {
		LogSDLError(std::cout, "CreateOpenGLContext");
		return false;
	}

	//
	// Initialise OpenGL
	//
	glClearColor(0,0,0,0);
	glClearDepth(1.0f);

	glViewport(0,0,m_ScreenWidth, m_ScreenHeight);

	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();

	glOrtho(0, m_ScreenWidth, m_ScreenHeight, 0, 1, -1);

	glMatrixMode(GL_MODELVIEW);

	glEnable(GL_TEXTURE_2D);

	glLoadIdentity();

	//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

	
	//!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

	//
	return true;
}


void Game::Shutdown() {
	SDL_GL_DeleteContext(m_SDL_GLcontext);

	SDL_DestroyRenderer(m_SDL_Renderer);
	SDL_DestroyWindow(m_SDL_Window);

	SDL_Quit();
}


void Game::Update() {
}


void Game::Render() {

	//render init
	glClearColor(0,0,0,0);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

	Draw* drawing = Draw::GetSingleton();

	drawing->Color3f(1, 0, 0);
	drawing->DrawTriangle(100, 100, 300, 100, 100, 300);
	drawing->DrawRectangle(10, 10, 400, 400);
	drawing->DrawCircle(200, 200, 50);
	
	//end of rendering
	SDL_RenderPresent(m_SDL_Renderer);
	SDL_GL_SwapWindow(m_SDL_Window);
}


void Game::OnEvent(SDL_Event* event) {
	if(event->type == SDL_QUIT) {
		m_Running = false;
	}
}


void Game::LogSDLError(std::ostream &os, const std::string &msg) {
	os << msg << " error: " << SDL_GetError() << std::endl;
}